aboutsummaryrefslogtreecommitdiff
path: root/src/routes/grocery/[id]/+page.svelte
blob: c74166be73bb6cba5cd80af3baf267289554edb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

<script lang="ts">
	import { enhance } from '$app/forms';

	let { data, form } = $props();
	let name = $state(data.item.name);
	let categoryId = $state(data.item.categoryId ?? -1);
	let saved = $state(false);

	$effect(() => {
		if (form?.success) {
			saved = true;
			const t = setTimeout(() => (saved = false), 2000);
			return () => clearTimeout(t);
		}
	});
	let reference = $state('');
	let price = $state('');
	let unit = $state('€/kg');
	let qualityScore = $state('');
</script>

<form
	class="form-card"
	method="POST"
	action="?/update"
	use:enhance={() => {
		return async ({ update }) => {
			await update({ reset: false });
		};
	}}
>
	<label>
		<span class="form-label">Nom</span>
		<input class="form-input" type="text" name="name" bind:value={name} />
	</label>
	<label>
		<span class="form-label">Catégorie</span>
		<select class="form-select" name="category_id" bind:value={categoryId}>
			<option value={-1}>—</option>
			{#each data.categories as cat (cat.id)}
				<option value={cat.id}>{cat.name}</option>
			{/each}
		</select>
	</label>
	<button class="btn-primary" type="submit"
		>{saved ? '✓ Enregistré' : 'Enregistrer'}</button
	>
</form>

<hr />

<h2>Références</h2>

<form class="form-card" method="POST" action="?/add" use:enhance>
	<label>
		<span class="form-label">Référence</span>
		<input
			class="form-input"
			type="text"
			name="reference"
			bind:value={reference}
			placeholder="ex. Carte Noire"
			autocomplete="off"
		/>
	</label>
	<div class="ref-row">
		<label>
			<span class="form-label">Prix</span>
			<input
				class="form-input"
				type="number"
				name="price"
				bind:value={price}
				placeholder="5.99"
				step="0.01"
			/>
		</label>
		<label>
			<span class="form-label">Unité</span>
			<select class="form-select" name="unit" bind:value={unit}>
				<option value="€/kg">€/kg</option>
				<option value="€/L">€/L</option>
				<option value="€/u">€/u</option>
			</select>
		</label>
		<label>
			<span class="form-label">Note</span>
			<input
				class="form-input"
				type="number"
				name="quality_score"
				bind:value={qualityScore}
				placeholder="0–100"
				min="0"
				max="100"
			/>
		</label>
	</div>
	<button class="btn-primary" type="submit">Ajouter une référence</button>
</form>

{#if form?.error}
	<p class="error">{form.error}</p>
{/if}

<form method="POST" action="?/delete" use:enhance>
	<ul class="item-list item-list--wide">
		{#each data?.references ?? [] as ref (ref.id)}
			<li class="item-row item-row--ref">
				<div class="ref-info">
					<strong>{ref.reference}</strong>
					{#if ref.price}
						<span class="ref-detail"
							>{ref.price.toFixed(2)}{ref.unit ? ` ${ref.unit}` : ''}</span
						>
					{/if}
					{#if ref.qualityScore != null}
						<span class="ref-detail">Note : {ref.qualityScore}/100</span>
					{/if}
				</div>
				<button
					class="btn-ghost"
					name="id"
					value={ref.id}
					type="submit"
					aria-label="Supprimer"
				>
					&times;
				</button>
			</li>
		{/each}
	</ul>
</form>

{#if (data?.references?.length ?? 0) === 0}
	<p class="empty">Aucune référence</p>
{/if}

<form method="POST" action="?/deleteItem" use:enhance>
	<button type="submit" class="delete-item-btn">Supprimer l'article</button>
</form>

<a href="/" class="back-link">&larr; Retour à la liste</a>

<style>
	.ref-row {
		display: flex;
		gap: 0.75rem;
		flex-wrap: wrap;
	}

	.ref-row label {
		flex: 1;
		min-width: 6rem;
	}

	.ref-info {
		flex-grow: 1;
		display: flex;
		flex-wrap: wrap;
		gap: 0.5rem;
		align-items: baseline;
	}

	.ref-detail {
		color: var(--text-secondary);
		font-size: 0.875rem;
		font-weight: 500;
		background: var(--bg);
		padding: 0.125rem 0.5rem;
		border-radius: 0.375rem;
	}

	.error {
		margin-top: -0.25rem;
		margin-bottom: 0.5rem;
	}

	.empty {
		margin-top: 0.75rem;
		padding: 1.5rem;
	}

	hr {
		border: none;
		border-top: 1.5px solid var(--border);
		margin: 1.5rem 0;
	}

	.back-link {
		display: inline-flex;
		align-items: center;
		gap: 0.375rem;
		margin-top: 1.5rem;
		color: var(--text-secondary);
		text-decoration: none;
		font-size: 0.875rem;
		font-weight: 500;
		transition: color 0.2s;
	}

	.back-link:hover {
		color: var(--accent);
	}

	.delete-item-btn {
		margin-top: 1.5rem;
		padding: 0.625rem 1.25rem;
		border: 1.5px solid var(--danger);
		border-radius: 0.75rem;
		background: var(--surface);
		color: var(--danger);
		font-size: 0.875rem;
		font-weight: 500;
		cursor: pointer;
		transition: all 0.2s ease;
	}

	.delete-item-btn:hover {
		background: var(--danger-bg);
		transform: translateY(-1px);
	}
</style>